//
//
//  @ Project : AgriMarketSystem
//  @ Date : 18/10/2013
//  @ Author : NTT
//
//

using System;
using System.Collections.Generic;
using System.Linq;
namespace AgriMarketService
{
    public class Article
    {

        protected int articleId;
        public int ArticleId
        {
            get { return articleId; }
            set { articleId = value; }
        }

        protected TypeOfArticle type;
        public TypeOfArticle Type
        {
            get { return type; }
            set { type = value; }
        }

        protected string topic;
        public string Topic
        {
            get { return topic; }
            set { topic = value; }
        }

        //protected int productId;
        //public int ProductId
        //{
        //    get { return productId; }
        //    set { productId = value; }
        //}
        protected Product product;

        public Product Product
        {
            get { return product; }
            set { product = value; }
        }


        protected int quantity;
        public int Quantity
        {
            get { return quantity; }
            set { quantity = value; }
        }

       

        protected Unit unit;
        public Unit Unit
        {
            get { return unit; }
            set { unit = value; }
        }

        protected decimal price;
        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }

        protected string description;
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        protected DateTime postedDate;
        public DateTime PostedDate
        {
            get { return postedDate; }
            set { postedDate = value; }
        }

        protected byte[] productImageBytes;
        public byte[] ProductImageBytes
        {
            get { return productImageBytes; }
            set { productImageBytes = value; }
        }

        protected string productImage;
        public string ProductImage
        {
            get { return productImage; }
            set { productImage = value; }
        }

        protected string authorId;
        public string AuthorId
        {
            get { return authorId; }
            set { authorId = value; }
        }

        //protected UserAccount author;

        //public UserAccount Author
        //{
        //    get { return author; }
        //    set { author = value; }
        //}


        public Article()
        {
        }
        public Article(int articleId,
            TypeOfArticle type,
            string topic,
            //int productId,
            Product product,
            int quantity,
            Unit unit,
            decimal price,
            string description,
            DateTime postedDate,
            byte[] productImageBytes,
            string productImage,
            string author
            //UserAccount author
            )
        {
            this.articleId = articleId;
            this.type = type;
            this.topic = topic;
            //this.productId = productId;
            this.quantity = quantity;
            this.unit = unit;
            this.price = price;
            this.description = description;
            this.postedDate = postedDate;
            this.productImageBytes = productImageBytes;
            this.productImage = productImage;
            this.authorId = author;
            //this.author = author;
        }
        public Article(int articleId,
            TypeOfArticle type,
            string topic,
            //int productId,
            Product product,
            int quantity,
            Unit unit,
            decimal price,
            string description,
            DateTime postedDate,
            string productImage,
            string author
            //UserAccount author
            )
        {
            this.articleId = articleId;
            this.type = type;
            this.topic = topic;
            //this.productId = productId;
            this.product = product;
            this.quantity = quantity;
            this.unit = unit;
            this.price = price;
            this.description = description;
            this.postedDate = postedDate;
            this.productImage = productImage;
            this.authorId = author;
            //this.author = author;
        }
        public static int AddNew(Article newArticle)
        {
            Tbl_Sell_Buy_Article newSell_Buy_Article = newArticle.ToTbl_Sell_Buy_Article();
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
//           Table_1 temp = (from temp1 in db.Table_1s where temp1.id == 1 select temp1).ToArray()[0];
            
            db.Tbl_Sell_Buy_Articles.InsertOnSubmit(newSell_Buy_Article);
            try
            {
                db.SubmitChanges();
                newArticle.articleId = newSell_Buy_Article.ArticleId;
                string[] splittedProductImage = newSell_Buy_Article.ProductImage.Split('.');
                newSell_Buy_Article.ProductImage = "Articles/"+newSell_Buy_Article.ArticleId.ToString() + "." + splittedProductImage[1];
                FileProvider.Upload(newArticle.productImageBytes, newSell_Buy_Article.ProductImage);
                db.SubmitChanges();
                return newArticle.articleId;
            }
            catch (Exception e)
            {
                return -1;//error
            }
        }
        public static int Update(Article updatedArticle)
        {

            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Sell_Buy_Article updatedSell_Buy_Article = (from tempSell_Buy_Article in db.Tbl_Sell_Buy_Articles
                                                            where tempSell_Buy_Article.ArticleId == updatedArticle.articleId
                                                            select tempSell_Buy_Article).FirstOrDefault();
            if (updatedSell_Buy_Article == null)
                return -1;//no exist
            updatedSell_Buy_Article.TypeId = updatedArticle.type.TypeId;
            updatedSell_Buy_Article.Topic = updatedArticle.topic;
            updatedSell_Buy_Article.ProductId = updatedArticle.product.ProductId;
            updatedSell_Buy_Article.Quantity = updatedArticle.quantity;
            updatedSell_Buy_Article.UnitId = updatedArticle.unit.UnitId;
            updatedSell_Buy_Article.Price = updatedArticle.price;
            updatedSell_Buy_Article.Description = updatedArticle.description;
            updatedSell_Buy_Article.PostedDate = updatedArticle.postedDate;
            updatedSell_Buy_Article.ProductImage = updatedArticle.productImage;
            updatedSell_Buy_Article.AuthorId = updatedArticle.authorId;
            if (updatedArticle.productImageBytes != null)
            {
                if (!FileProvider.Upload(updatedArticle.productImageBytes, updatedArticle.productImage))
                    return 1;
            }
            try
            {
                db.SubmitChanges();
                return 0;
            }
            catch (Exception e)
            {
                return 1;//error
            }
        }
        public static int DeleteByArticleId(int deletedArticleId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Sell_Buy_Article deletedSell_Buy_Article = (from tempSell_Buy_Article in db.Tbl_Sell_Buy_Articles
                                                            where tempSell_Buy_Article.ArticleId == deletedArticleId
                                                            select tempSell_Buy_Article).FirstOrDefault();
            if (deletedSell_Buy_Article == null)
                return -1; //no exist
            if (!ArticleComment.DeleteAllCommentsByArticleId(deletedArticleId))
                return 1;//error
            db.Tbl_Sell_Buy_Articles.DeleteOnSubmit(deletedSell_Buy_Article);
            try
            {
                FileProvider.Delete(deletedSell_Buy_Article.ProductImage);
                db.SubmitChanges();
                return 0;
            }
            catch (Exception e)
            {
                return 1;//error
            }
        }
        public static List<Article> GetAllNewArticles()
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            List<Article> result = (from tempArticle in db.Tbl_Sell_Buy_Articles
                                    join tempType in db.Tbl_TypeOfArticles on tempArticle.TypeId equals tempType.TypeId
                                    join tempUnit in db.Tbl_Units on tempArticle.UnitId equals tempUnit.UnitId
                                    select new Article(tempArticle.ArticleId,
                                        new TypeOfArticle(tempType.TypeId, tempType.Type),
                                        tempArticle.Topic,
                                        Product.GetProductById(tempArticle.ProductId),
                                        tempArticle.Quantity,
                                        new Unit(tempUnit.UnitId, tempUnit.UnitName),
                                        tempArticle.Price,
                                        tempArticle.Description,
                                        tempArticle.PostedDate,
                                        tempArticle.ProductImage,
                                        tempArticle.AuthorId
                                        )).ToList();
            return result;
        }
        public Tbl_Sell_Buy_Article ToTbl_Sell_Buy_Article()
        {
            return new Tbl_Sell_Buy_Article()
            {
                ArticleId = this.articleId,
                TypeId = this.type.TypeId,
                Topic = this.Topic,
                ProductId = this.product.ProductId,
                Quantity = this.quantity,
                UnitId = this.unit.UnitId,
                Price = this.price,
                Description = this.description,
                PostedDate = this.postedDate,
                ProductImage = this.productImage,
                AuthorId = this.authorId
            };
        }
        public static Article GetAnArticleByArticleId(int articleId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            return (from tempArticle in db.Tbl_Sell_Buy_Articles
                    join tempType in db.Tbl_TypeOfArticles on tempArticle.TypeId equals tempType.TypeId
                    join tempUnit in db.Tbl_Units on tempArticle.UnitId equals tempUnit.UnitId
                    where tempArticle.ArticleId == articleId
                    select new Article(tempArticle.ArticleId,
                        new TypeOfArticle(tempType.TypeId, tempType.Type),
                        tempArticle.Topic,
                        Product.GetProductById(tempArticle.ProductId),
                        tempArticle.Quantity,
                        new Unit(tempUnit.UnitId, tempUnit.UnitName),
                        tempArticle.Price,
                        tempArticle.Description,
                        tempArticle.PostedDate,
                        tempArticle.ProductImage,
                        tempArticle.AuthorId
                        )).FirstOrDefault();
        }
    }
}